package com.kplan.plan.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.kplan.plan.dto.TrainTimeDto;
import com.kplan.plan.service.PlanService;
import com.kplan.util.Encoder;

@WebServlet("/plan")
public class PlanController extends HttpServlet {
	private static final long serialVersionUID = 1L;
    

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		excute(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("EUC-KR");
		excute(request, response);
	}

	private void excute(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		String root = request.getContextPath();
		
		String act = request.getParameter("act");
		
		String path = "/index.jsp";
		
		boolean flag = true;
		if("sctrain".equals(act)) {
			String startTime = request.getParameter("startTime");
			String division = Encoder.isoToEuc(request.getParameter("division"));
			String startStation = Encoder.isoToEuc(request.getParameter("startStation"));
			String endStation = Encoder.isoToEuc(request.getParameter("endStation"));
			
			List<TrainTimeDto> trainsearch = PlanService.getInstance().trainSearch(startTime, division, startStation, endStation);
//			request.setAttribute("tSearch", trainsearch);

			if(trainsearch==null){
//				path = "/plan/planMain.jsp";
				path = "";
			} else {
				request.setAttribute("tSearch", trainsearch);
				flag = false;
				path = "/plan/mk_test2.jsp";
			}
			

			
			
			
			
		} else if("".equals(act)) {
			path = "/plan/planMain.jsp";
		} else if("".equals(act)) {
			path = "/plan/planMain.jsp";
		} else if("".equals(act)) {
			path = "/plan/planMain.jsp";
		}
		
		if(flag)
			response.sendRedirect(root + path);
		else {
			RequestDispatcher disp = request.getRequestDispatcher(path);
			disp.forward(request, response);
		}
	}
}
